<?php

/**
 * This is the model class for table "tbl_contract_equipment".
 *
 * The followings are the available columns in table 'tbl_contract_equipment':
 * @property integer $id
 * @property integer $equipmentid
 * @property string $contractid
 * @property string $qty
 * @property string $price
 * @property integer $planid
 * @property integer $pmr
 */
class ContractEquipment extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @return ContractEquipment the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_contract_equipment';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('equipmentid, prorate, prorate_notes,contractid, qty, price, planid, pmr,mfg,serial,model', 'safe'),
            array('equipmentid, pmr,sl', 'numerical', 'integerOnly' => true),
            array('prorate', 'length', 'max' => 12),
            array('contractid', 'length', 'max' => 41),
            
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, equipmentid, contractid, qty, price, planid, pmr', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'original'=>array(self::BELONGS_TO,'Equipment','equipmentid'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'equipmentid' => 'Equipmentid',
            'contractid' => 'Contractid',
            'qty' => 'Qty',
            'price' => 'Price',
            'planid' => 'Planid',
            'pmr' => 'Pmr',
        );
    }

    public function getPlanTypeText()
    {
        return isset($this->planTypeList[$this->planid]) ? $this->planTypeList[$this->planid] : NULL;
    }

    public function getPlanTypeList()
    {
        $data = Yii::app()->db->createCommand('select * from tbl_plantypes')->queryAll();
        if (count($data) > 0) {
            $rs = array();
            foreach ($data as $i => $item) {
                $rs[$item['id']] = $item['plantype'];
            }
            return $rs;
        }
        return NULL;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('equipmentid', $this->equipmentid);
        $criteria->compare('contractid', $this->contractid, true);
        $criteria->compare('qty', $this->qty, true);
        $criteria->compare('price', $this->price, true);
        $criteria->compare('planid', $this->planid);
        $criteria->compare('pmr', $this->pmr);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
}
